<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/11/26
 * Time: 11:18
 */

namespace app\api\controller;


use app\common\controller\Api;

use think\Cache;
use think\Db;
use think\Exception;
use think\session\driver\Redis;

/*
 *购物车接口
 */
class Gouwu extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $token = '';
    protected $userid = '';

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->token = $_REQUEST["token"]??'';
        if ($this->token){
            $this->userid = Db::name("user_token")->where("token",'eq',$this->token)->value("user_id");
            if (!$this->userid){
                $this->error("token不存在");
            }
        }else{
            $this->error("请先登录",1002);
        }
    }
    /**
     * 添加/减少/移除 购物车
     * @ApiTitle    (添加/减少/移除 购物车)
     * @ApiSummary  (添加/减少/移除 购物车)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Gouwu/addGouWuChe)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiHeaders  (name=goods_id, type=int, required=true, description="商品id")
     * @ApiHeaders  (name=chicun, type=int, required=true, description="商品尺寸id")
     * @ApiHeaders  (name=type, type=int, required=true, description="添加1  删减2 移除3")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function addGouWuChe(){
        Db::startTrans();
        try{
            $goods_id = $_REQUEST['goods_id']??'';
            $chicun = $_REQUEST['chicun_id']??'';
            $mount = $_REQUEST['mount']??1;//减少传负数
            if (empty($goods_id) || empty($chicun)){
                $this->error("请选择商品或者选好尺寸");
            }
            $where = array(
                "goods_id"=>$goods_id,
                "user_id"=>$this->userid,
                "chicun"=>$chicun,
            );

            $res = Db::name("gouwuche")->where($where)->find();
            if (!$res){
                //购物车为空
                if ($mount>0){
                    $where["mount"] = $mount;
                    $where['ctime'] = time();
                    $where['utime'] = time();
                    Db::name("gouwuche")->insert($where);
                    Db::commit();
                    $this->success();
                }else{
                    $this->error("您还未添加该商品");
                }

            }else {
                //购物车不为空
                $cha = $res['mount'] + $mount;
                if ($cha > 0){
                    Db::name("gouwuche")->where($where)->setInc('mount',$mount);
                    Db::name("gouwuche")->where($where)->update(array("utime"=>time()));
                    Db::commit();
                    $this->success();
                }else if ($cha == 0){
                    Db::name("gouwuche")->where($where)->delete();
                    Db::commit();
                    $this->success();
                }
            }
        }catch (Exception $e){
            Db::rollback();
            $this->error();
        }
    }
    /**
     * 购物车列表
     * @ApiTitle    (购物车列表)
     * @ApiSummary  (购物车列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Gouwu/gouWuCheLists)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function gouWuCheLists(){
        $lists = Db::name("gouwuche")->where("user_id",$this->userid)->select();
        foreach ($lists as &$va){
            $goodInfo = Db::name("goods")->where("id",$va['goods_id'])->find();
            //计算总价
            $va['old_money'] = round($va['mount'] * $goodInfo['old_price'],2);
            $va['goods_detail'] = $goodInfo;
            $va['new_money'] = round($va['mount'] * $goodInfo['new_price'], 2);;
            $chicunInfo = Db::name("chicun")->where("id",$va['chicun'])->find();
            $va['chicun_name'] = $chicunInfo['name'];
        }
         if ($lists){
             $this->success('操作成功',$lists);
         }else{
             $this->success('暂无数据');
         }
    }
}